#include "cv.h"
#include "highgui.h"
#include <iostream>

using namespace std;

/*
Displays streaming video.  When "esc" button is pressed, picture is taken and displayed until any button is pressed and saved as "snap.jpg".

example in terminal:

./snapshot

OR (if it worked, which it doesn't)

./snapshot video.avi


Note: There is a flaw in OpenCV so the second command won't work
*/

int main( int argc, char** argv ) {

	cvNamedWindow( "Example2", CV_WINDOW_AUTOSIZE );
	CvCapture* capture;
	if(argc == 1){
		capture = cvCreateCameraCapture(-1);	
	}
	else{
		capture = cvCreateFileCapture(argv[1]);
	}

	IplImage* frame;
	int i = 0;
	while(1) {
		frame = cvQueryFrame( capture );
		//if (!frame){cout <<"Error :(" ;break;}
		cvShowImage("Example2", frame);
		char c = cvWaitKey(33);
		if (c == 27) break; //if esc (27 = esc in ASCII) is pressed, loop exits		
	}
	cvSaveImage("snap.jpg", frame);
	cvShowImage("snap.jpg", frame);
	cvWaitKey(0);
	cvReleaseImage( &frame );
	cvDestroyWindow("Example2");
	cvReleaseCapture( &capture );
}
